package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.memberrecharge;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.CreateRechargeOrderBean;
import com.zzsy.carosshopprojects.bean.OrderResultBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import androidx.lifecycle.MutableLiveData;

public class RechargePayTwoMode extends BaseViewMode {

    private MutableLiveData<OrderResultBean> liveOrder;

    public MutableLiveData<OrderResultBean> getLiveOrderData() {
        if (liveOrder == null)
            liveOrder = new MutableLiveData<>();
        return liveOrder;
    }

    /**
     * 去支付
     * type 1，支付宝 2. 微信 3 银行卡 5 现金支付
     *
     * @param context
     */
    public void toPay(Activity context, String couponIds, String amount, String serviceCardIds, String activityId, String activityPositionId, String memberCardId
            , String memberId, String payType, String phone, String receiveAmount,
                      String productId) {
        RequestUtils.getToken(context, new MyObserver<BaseBean>(context, true) {
            @Override
            public void onSuccess(BaseBean result) {

            }

            @Override
            public void onFailure(Throwable e, String tokenMsg, int code) {
                if (code == ConfigureFile.code) {
                    String couponIdSub = "";
                    if (!StringUtils.isEmpty(couponIds)) {
                        couponIdSub = couponIds.substring(1);
                    }

                    String serviceCardSub = "";
                    if (!StringUtils.isEmpty(serviceCardIds)) {
                        serviceCardSub = serviceCardIds.substring(1);
                    }

                    String amountStr = ConverterHelper.toBigDecimal(amount).multiply(ConverterHelper.toBigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP).toString();
                    Map<String, String> map = new HashMap<>();
                    map.put("activityId", activityId);
                    map.put("activityPositionId", activityPositionId);
                    map.put("amount", amountStr);
                    map.put("companyId", DataUtil.getInstance().getCompanyId());
                    map.put("couponIds", couponIdSub);
                    map.put("memberCardId", memberCardId);
                    map.put("memberId", memberId);
                    map.put("orderAmount", amountStr);
                    map.put("payType", payType);
                    map.put("phone", phone);
                    map.put("source", "3");
                    map.put("staffId", "");
                    map.put("storeId", DataUtil.getInstance().getStoreId());
                    map.put("terminalId", DataUtil.getInstance().getTerminalId());
                    map.put("serviceCardIds", serviceCardSub);
                    map.put("invoiceSwitch", "0");
                    map.put("receiveAmount", receiveAmount);
                    map.put("productId", productId);
                    Log.d("xxxxx", map.toString());
                    CreateOrder(context, map, tokenMsg);
                } else {
                    ToastUtils.showToast(context, tokenMsg);
                    context.finish();
                }

            }
        });
    }

    /**
     * 创建支付订单
     *
     * @param context
     * @param map
     */
    private void CreateOrder(Context context, Map<String, String> map, String tokenMsg) {

        RequestUtils.CreateRechargeOrder(context, map, tokenMsg, new MyObserver<CreateRechargeOrderBean>(context) {
            @Override
            public void onSuccess(CreateRechargeOrderBean bean) {
                OrderResultBean orderResultBean = new OrderResultBean();
                if (map.get("payType").equals("1"))
                    orderResultBean.setType(ConfigureFile.ZFBPAY);
                else if (map.get("payType").equals("2"))
                    orderResultBean.setType(ConfigureFile.WXPAY);
                else
                    orderResultBean.setType(map.get("payType"));
                orderResultBean.setOrderNumber(bean.getRechargeNumber());
                orderResultBean.setPayAmount(bean.getAmount());
                liveOrder.postValue(orderResultBean);

            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context, "订单创建异常");
                liveOrder.postValue(null);
            }
        });
    }
}
